﻿var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;
var imgSizeLimit = 300;
var swfSizeLimit = "2MB";
function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $("#bh-head-xianshi").css('display', 'block');
}
function jsddm_close() {
    if (ddmenuitem)
        ddmenuitem.css('display', 'none');
}
function jsddm_timer() {
    closetimer = window.setTimeout(jsddm_close, timeout); 
}
function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    } 
}
$(document).ready(function() {
    $('#showtext2').bind('mouseover', jsddm_open);
    $('#showtext2').bind('mouseout', jsddm_timer);
});

var id;
//下拉列表
$(document).click(function(event) {
    var $target = $(event.target);
    if ($target.hasClass("dropdown")) {
        $target.children("ul").slideToggle("fast");
        $target.siblings().children("ul").slideUp("fast");
    } else if (!($target.is("li") && $target.parents("li.dropdown").length > 0)) {
        $(".dropdown").children("ul").slideUp("fast");
    }
    if ($target.is("a") && $target.parents("li.dropdown").length > 0) {
        var dvalue = $target.parents("li.dropdown").children("span");
        dvalue.text($target.text());
        dvalue.attr("value", $target.attr("value"));
    }

});

//判断是否为空
function isEmpty(id) {
	var text = $("#" + id).val();
	if ($.trim(text)=="") {
		return true;
	} else {
		return false;
	}
	
}
//判断邮件地址是否合法
function isEmail(str) {
	var res = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ ;
	var re = new RegExp(res);
	return !(str.match(re) == null);
}

//返回val的字节长度
function getByteLen(val) {
    var len = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null) //全角
            len += 2;
        else
            len += 1;
    }
    return len;
}

//返回val在规定字节长度max内的值
function getByteVal(val, max) {
    var returnValue = '';
    var byteValLen = 0;
    for (var i = 0; i < val.length; i++) {
        if (val[i].match(/[^\x00-\xff]/ig) != null)
            byteValLen += 2;
        else
            byteValLen += 1;

        if (byteValLen > max)
            break;

        returnValue += val[i];
    }
    return returnValue;
}


//array to json
$.extend({
    arrayToJson: function(itemList) {
        if (itemList.length == 0) return "";
        var jsonStr = "[";
        for (var i = 0; i < itemList.length; i++) {
            var str = "{";
            for (var key in itemList[i]) {
                if (key != undefined) {
                    var value = itemList[i][key];
                    if (typeof value != "function") {
                        if (typeof value == "object" && value != null) {
                            str += ('"' + key + '"' + ':' + this.objectToJosn(value) + ',');      //数组下包含数组
                        }
                        else {
                            str += ('"' + key + '"' + ':"' + $.string2Json(value) + '",');
                        }
                    }
                }
            }
            jsonStr += (str.substring(0, str.length - 1) + "},");
        }
        if (jsonStr.length == 1) {
            var str = "{";
            for (var key in itemList) {
                if (key != undefined) {
                    var value = itemList[key];
                    if (typeof value != "function") {
                        if (typeof value == "object" && value != null) {
                            str += (key + ':' + this.objectToJosn(value) + ',');      //数组下包含数组
                        }
                        else {
                            str += (key + ':"' + $.string2Json(value) + '",');
                        }
                    }
                }
            }
            jsonStr += (str.substring(0, str.length - 1) + "},");
        }
        return (jsonStr.substring(0, jsonStr.length - 1) + "]");
    },
    objectToJson: function(item) {
        var str = "{";
        for (var key in item) {
            if (key != undefined) {
                var value = item[key];
                if (typeof value != "function") {
                    if (typeof value == "object" && value != null) {
                        str += ('"' + key + '"' + ':' + this.objectToJosn(value) + ',');            //对象下包含对象
                    }
                    else {
                        str += ('"' + key + '"' + ':"' + $.string2Json(value) + '",');
                    }
                }
            }
        }
        return (str.substring(0, str.length - 1) + "}");
    },
    string2Json: function(value)            //特殊字符进行处理
    {
        value += "";
        value = value.replace(/\\/g, "\\\\");   // \换\\
        value = value.replace(/\"/g, "\\\"");   // "换\"
        value = value.replace(/\//g, "\\\/");   // /换\/
        //value = value.replace(/\b/g, "\\b");   // b换\b
        //value = value.replace(/\f/g, "\\f/");   // f换\f
        value = value.replace(/\n/g, "\\n/");   // n换\n
        value = value.replace(/\r/g, "\\r/");   // r换\r
        value = value.replace(/\t/g, "\\t/");   // t换\t
        return value;
    }
});

//全局替换
String.prototype.replaceAll = function(AFindText, ARepText) {
    var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g, "\\$1"), "ig");
    return this.replace(raRegExp, ARepText);
}

//得到URL参数值，若无，返回null
function QueryString(fieldName) {
    var urlString = document.location.href;
    if (urlString != null) {
        var typeQu = fieldName + "=";
        var urlEnd = urlString.indexOf(typeQu);
        if (urlEnd != -1) {
            var paramsUrl = urlString.substring(urlEnd + typeQu.length);
            var isEnd = paramsUrl.indexOf('&');
            if (isEnd != -1) {
                return paramsUrl.substring(0, isEnd).replaceAll("#", "");
            }
            else {
                return paramsUrl.replaceAll("#", "");
            }
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}